package cn.wolfcode.shop;

import cn.wolfcode.shop.domain.SkuProperty;
import cn.wolfcode.shop.service.ICataLogService;
import cn.wolfcode.shop.service.ISkuPropertyService;
import cn.wolfcode.shop.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SkuPropertyController {
    @Reference
    private ISkuPropertyService skuPropertyService;
    @Reference
    private ICataLogService cataLogService;

    @RequestMapping("/skuProperty")
    public String skuPropertyPage(Model model){
        model.addAttribute("allCatalog", cataLogService.SelectAllCache());
        return "skuProperty/skuProperty";
    }

    @RequestMapping("/skuProperty/get/{catalogId}")
    public String skuPropertyListPage(@PathVariable("catalogId") Long catalogId, Model model){
        model.addAttribute("skuPropertyList", skuPropertyService.listByCatalogId(catalogId));
        return "skuProperty/skuProperty_list";
    }

    @RequestMapping("/skuProperty/save")
    @ResponseBody
    public JSONResultVo saveOrUpdate(SkuProperty skuProperty){
        JSONResultVo vo = new JSONResultVo();
        try {
            skuPropertyService.saveOrUpdate(skuProperty);
        } catch (Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }

    @RequestMapping("/skuProperty/delete/{id}")
    @ResponseBody
    public JSONResultVo delete(@PathVariable("id") Long id){
        JSONResultVo vo = new JSONResultVo();
        try {
            skuPropertyService.delete(id);
        } catch (Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }

}
